const { extend, Toast, Tab } = require('../../zanui/index')
import { HOST } from '../../config/apiConfig.js'

//获取应用实例
const app = getApp()

Page(extend({}, Tab, {
  data: {
    tab: {
      list: [{
        id: 'scenic',
        title: '景区'
      }, {
        id: 'hotel',
        title: '酒店'
      }, {
        id: 'store',
        title: '餐饮'
      }, {
        id: 'baseData',
        title: '基础数据'
      }],
      selectedId: 'scenic',
      componentId: 'tab',
      height: 15
    },
    scenicData: [],
    hotelData: [],
    storeData: [],
    two: [{
      "e": "111",
      "f": "222"
    }, {
      "e": "333",
      "f": "444"
    }],
    scenic: true,
    hotel: false,
    store: false,
    baseData: false
  },
  onLoad: function () {
    // 设置当前页标题
    wx.setNavigationBarTitle({
      title: '数据列表'
    })
  },
  onShow: function () {
    const userType = app.globalData.userInfo.userType
    if (userType != 2) {
      this.setData({
        tab: {
          list: [{
            id: 'scenic',
            title: '景区'
          }, {
            id: 'hotel',
            title: '酒店'
          }, {
            id: 'store',
            title: '餐饮'
          }],
          selectedId: 'scenic',
          componentId: 'tab',
          height: 15
        }
      })
    }
    this.getScenicData()
    this.getHotelData()
    this.getStoreData()
  },
  // zanui tab切换事件
  handleZanTabChange(e) {
    // 设置tab选中状态
    var componentId = e.componentId;
    var selectedId = e.selectedId;
    this.setData({
      [`${componentId}.selectedId`]: selectedId
    });

    // 控制tab下面页面的显示隐藏
    if (selectedId == 'scenic') {
      this.setData({
        scenic: true,
        hotel: false,
        store: false,
        baseData: false
      })
    } else if (selectedId == 'hotel') {
      this.setData({
        hotel: true,
        scenic: false,
        store: false,
        baseData: false
      })
    } else if (selectedId == 'store') {
      this.setData({
        store: true,
        hotel: false,
        scenic: false,
        baseData: false
      })
    } else if (selectedId == 'baseData') {
      this.setData({
        store: false,
        hotel: false,
        scenic: false,
        baseData: true
      })
    }
  },
  // 景区tab 点击事件
  showScenicDetail: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../scenicBase/scenicBase?id=' + id
    })
  },
  // 酒店tab 点击事件
  showHotelDetail: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../hotel/hotel?id=' + id + '&isHotelNavigation=true',
    })
  },
  // 餐饮tab 点击事件
  showCateringDetail: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../catering/catering?id=' + id,
    })
  },
  // 添加数据按钮 点击事件
  addScenicData: function () {
    wx.navigateTo({
      url: '../scenicBase/scenicBase',
    })
  },
  addHotelData: function () {
    wx.navigateTo({
      url: '../hotel/hotel',
      //url: '../hotelRoom/hotelRoom',
    })
  },
  addCateringData: function () {
    wx.navigateTo({
      url: '../catering/catering',
    })
  },
  // 获取景区列表
  getScenicData: function () {
    const _self = this
    // 获取景区数据
    wx.request({
      url: HOST + '/v1/scenic?areaId=' + app.globalData.areaInfo.id,
      method: 'GET',
      success: (res) => {
        // 停止下拉刷新动画
        wx.stopPullDownRefresh()
        const data = res.data.data
        for (let i = 0; i < data.length; i++) {
          if (data[i].scenicImages != undefined) {
            let index = data[i].scenicImages.indexOf(";")
            if (index != -1) {
              data[i].scenicImages = data[i].scenicImages.substring(0, index)
            }
          }
        }
        _self.setData({
          scenicData: data
        })
      }
    })
  },
  // 获取酒店列表
  getHotelData: function () {
    const _self = this
    // 获取酒店数据
    wx.request({
      url: HOST + '/v1/hotel?areaId=' + app.globalData.areaInfo.id,
      method: 'GET',
      success: (res) => {
        wx.stopPullDownRefresh()
        const data = res.data.data
        for (let i = 0; i < data.length; i++) {
          if (data[i].hotelImages != undefined) {
            let index = data[i].hotelImages.indexOf(";")
            if (index != -1) {
              data[i].hotelImages = data[i].hotelImages.substring(0, index)
            }
          }
        }
        _self.setData({
          hotelData: data
        })
      }
    })
  },
  // 获取餐饮列表
  getStoreData: function () {
    const _self = this
    // 获取餐饮数据
    wx.request({
      url: HOST + '/v1/catering?areaId=' + app.globalData.areaInfo.id,
      method: 'GET',
      success: (res) => {
        wx.stopPullDownRefresh()
        const data = res.data.data
        for (let i = 0; i < data.length; i++) {
          if (data[i].storeImages != undefined) {
            let index = data[i].storeImages.indexOf(";")
            if (index != -1) {
              data[i].storeImages = data[i].storeImages.substring(0, index)
            }
          }
        }
        _self.setData({
          storeData: data
        })
      }
    })
  },
  // 下拉刷新
  onPullDownRefresh() {
    if (this.data.scenic) {
      this.getScenicData()
    } else if (this.data.store) {
      this.getStoreData()
    } else if (this.data.hotel) {
      this.getHotelData()
    } else {
      wx.stopPullDownRefresh()
    }
  },
  // 酒店设施管理
  hotelFacilityManage: function () {
    wx.navigateTo({
      url: '../hotelFacility/hotelFacility',
    })
  },
  // 酒店服务管理
  hotelServiceManage: function () {
    wx.navigateTo({
      url: '../hotelService/hotelService',
    })
  },
  // 酒店星级管理
  hotelLevelManage: function () {
    wx.navigateTo({
      url: '../hotelLevel/hotelLevel',
    })
  },
  // 房间设施管理
  roomFacilityManage: function () {
    wx.navigateTo({
      url: '../roomFacility/roomFacility',
    })
  },
  // 餐饮设施管理
  storeFacilityManage: function () {
    wx.navigateTo({
      url: '../storeFacility/storeFacility',
    })
  },
  // 支付方式管理
  payTypeManage: function () {
    wx.navigateTo({
      url: '../payType/payType',
    })
  },
  // 酒店类型管理
  hotelTypeManage: function () {
    wx.navigateTo({
      url: '../hotelType/hotelType',
    })
  }
}))
